package deibay.client.gui;

import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;

import org.jdesktop.swingx.JXDatePicker;
import org.jdesktop.swingx.JXFrame;

import deibay.client.core.views.IViewItemDetailsSeller;

public class CreateItemView extends JXFrame implements IViewItemDetailsSeller{	
	
	private JTextField nameField = null;
	private JTextArea descriptionField = null;
	private JTextField categoryField = null;
	private JTextField sPriceField = null;
	private JScrollPane descriptionScroll = null;
	private JLabel nameLabel = null;
	private JLabel descriptionLabel = null;
	private JLabel categoryLabel = null;
	private JLabel startPriceLabel = null;
	private JLabel deadlineLabel = null;
	private JButton submitButton = null;
	private JPanel main = null;
	private JXDatePicker datePicker = null;
	private SimpleDateFormat dateFormat = null;  //  @jve:decl-index=0:
	
	public CreateItemView()
	{
		super();
		initialize();
	}
	
	private void initialize()
	{		
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		
		dateFormat = new SimpleDateFormat("d MM yyyy HH:mm:ss");
		startPriceLabel = new JLabel();
		startPriceLabel.setBounds(new Rectangle(30, 220, 100, 20));
		startPriceLabel.setText("StartPrice ");		
		categoryLabel = new JLabel();
		categoryLabel.setBounds(new Rectangle(30, 170, 100, 20));
		categoryLabel.setText("Category ");
		descriptionLabel = new JLabel();
		descriptionLabel.setBounds(new Rectangle(30, 80, 100, 20));
		descriptionLabel.setText("Description ");
		nameLabel = new JLabel();
		nameLabel.setBounds(new Rectangle(30, 30, 100, 20));
		nameLabel.setText("Name ");
		deadlineLabel = new JLabel();
		deadlineLabel.setBounds(new Rectangle(30, 270, 100, 20));
		deadlineLabel.setText("Deadline Time ");
		setTitle("Add Item");		
		setSize(new Dimension(520, 520));
		setContentPane(getMainPane());
		setVisible(true);
	}		

	private JPanel getMainPane() {
		if(main == null)
		{
			main = new JPanel();
			main.setSize(520, 520);
			main.setLayout(null);
			main.add(getNameField(), null);
			main.add(getDescriptionScroll(), null);
			main.add(getCategoryField(), null);
			main.add(getSPriceField(), null);			
			main.add(nameLabel, null);
			main.add(descriptionLabel, null);
			main.add(categoryLabel, null);			
			main.add(startPriceLabel, null);
			main.add(deadlineLabel, null);	
			main.add(getDatePicker(), null);
			main.add(getSubmitButton(), null);
		}
		return main;
	}

	private JXDatePicker getDatePicker() {
		if(datePicker == null)
		{
			datePicker = new JXDatePicker(System.currentTimeMillis()+86400000);
			datePicker.setBounds(new Rectangle(160, 270, 250, 20));
			datePicker.setFormats(dateFormat);			
		}
		return datePicker;
	}

	private JButton getSubmitButton() {
		if(submitButton == null)
		{
			submitButton = new JButton();
			submitButton.setText("Add");
			submitButton.setBounds(new Rectangle(160, 320, 80, 20));
		}
		return this.submitButton;
	}

	private JScrollPane getDescriptionScroll() {
		if(descriptionScroll == null)
		{
			descriptionScroll = new JScrollPane();
			descriptionScroll.setBounds(new Rectangle(160, 80, 250, 60));
			descriptionScroll.setViewportView(getDescriptionField());
		}
		return descriptionScroll;
	}
	
	private JTextField getSPriceField() {
		if(sPriceField == null)
		{
			sPriceField = new JTextField();
			sPriceField.setBounds(new Rectangle(160, 220, 250, 20));			
		}
		return sPriceField;
	}
	
	private JTextField getCategoryField() {
		if(categoryField == null)
		{
			categoryField = new JTextField();
			categoryField.setBounds(new Rectangle(160, 170, 250, 20));			
		}
		return categoryField;
	}
	
	private JTextArea getDescriptionField() {
		if(descriptionField == null)
		{
			descriptionField = new JTextArea();			
		}
		return descriptionField;
	}
	
	private JTextField getNameField() {
		if(nameField == null)
		{
			nameField = new JTextField();
			nameField.setBounds(new Rectangle(160, 30, 250, 20));			
		}
		return nameField;
	}	
	
	public String getItemCategory() {
		return categoryField.getText();
	}

	public Date getItemDeadline() {
		return new Date(datePicker.getDateInMillis());				
	}

	public String getItemDescription() {
		return descriptionField.getText();
	}

	public int getItemStartPrice() {
		return Integer.parseInt(this.sPriceField.getText());
	}

	public void setAddItemAction(ActionListener listener) {		
		this.submitButton.addActionListener(listener);
	}

	public void setAddItemActionEnabled(boolean b) {		
		this.submitButton.setEnabled(b);
	}

	public void close()
	{
		this.setVisible(false);		
	}

	public String getItemName() {		
		return nameField.getText();
	}
}
